/**中小学 */

import {
  API_getClassList,
  API_getClassMine,
  API_getDepartmentList,
  API_getDepartmentStaffs,
  API_getGradeList,
  API_getGroupList,
  API_getGroupMine,
  API_getIdentityList,
  API_getMyCollegeTeacher,
  API_getUserIdentity,
  API_getUserParents,
  API_getUserStudents,
  API_getUserTeachers,
} from "../../../../network/servers";
import getInfo from "../../utils/getInfo";

export const getNewData2 = {
  getIdentity(data, receivers, fnSet) {
    return new Promise<any>((resolve, reject) => {
      let identities = receivers?.identities;
      let persons = receivers?.persons;
      if (data.type.length == 1) {
        let params = {
          schoolId: getInfo.getSchoolId(),
          userType: "0,11",
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getIdentityList(params, header).then((res) => {
          console.log(
            "API-获取管理员身份列表（中小学）:",
            res,
            "\nparams:",
            params
          );
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = identities?.findIndex(
                (pItem) => pItem.identityCode == item.identityCode
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                //有选中,去掉接口
                identities.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.identityName,
                subData: undefined,
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      } else if (data.type.length == 2) {
        let params = {
          schoolId: getInfo.getSchoolId(),
          identityCode: data.Info.identityCode,
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getUserIdentity(params, header).then((res) => {
          console.log(
            "API-获取管理员用户（中小学）:",
            res,
            "\nparams:",
            params
          );
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = persons?.findIndex(
                (pItem) => pItem.userId == item.userId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                persons.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.userName,
                subData: [],
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      }
    });
  },

  getTeachers(data, receivers, fnSet) {
    return new Promise<any>((resolve, reject) => {
      let teachers = receivers?.teachers;
      let persons = receivers?.persons;
      if (data.type.length == 1) {
        let params = {
          schoolType: 2,
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getGroupList(params, header).then((res) => {
          console.log(
            "API-获取教学组列表（中小学）:",
            res,
            "\nparams:",
            params
          );
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = teachers?.findIndex(
                (pItem) => pItem.subjectId == item.subjectId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                //有选中,去掉接口
                teachers.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.subjectName + "教师组",
                subData: undefined,
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      } else if (data.type.length == 2) {
        let params = {
          schoolType: 2,
          groupId: data.Info.subjectId,
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getUserTeachers(params, header).then((res) => {
          console.log("API-获取教师用户（中小学）:", res, "\nparams:", params);
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = persons?.findIndex(
                (pItem) => pItem.userId == item.userId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                persons.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.userName,
                subData: [],
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      }
    });
  },

  getStudents(data, receivers, fnSet) {
    return new Promise<any>((resolve, reject) => {
      let students = receivers?.students;
      let persons = receivers?.persons;
      if (data.type.length == 1) {
        let params = {
          schoolType: getInfo.getSchoolType(),
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getGradeList(params, header).then((res) => {
          console.log("API-获取年级列表（中小学）:", res, "\nparams:", params);
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = students?.findIndex(
                (pItem) => pItem.gradeId == item.gradeId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                //有选中,去掉接口
                students.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.gradeName,
                subData: undefined,
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      } else if (data.type.length == 2) {
        let params = {
          schoolType: 2,
          sysId: getInfo.getSysId(),
          gradeId: data.Info.gradeId,
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getClassList(params, header).then((res) => {
          console.log("API-获取班级列表（中小学）:", res, "\nparams:", params);
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = students?.findIndex(
                (pItem) => pItem.classId == item.classId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                //有选中,去掉接口
                students.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.className,
                subData: undefined,
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      } else if (data.type.length == 3) {
        let params = {
          schoolType: getInfo.getSchoolType(),
          classId: data.Info.classId,
          schoolId: getInfo.getSchoolId(),
          // classType:data.Info.classType
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getUserStudents(params, header).then((res) => {
          console.log("API-获取学生用户（中小学）:", res, "\nparams:", params);
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = persons?.findIndex(
                (pItem) => pItem.userId == item.userId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                //有选中,去掉接口
                persons.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.userName,
                subData: [],
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      }
    });
  },

  getParents(data, receivers, fnSet) {
    return new Promise<any>((resolve, reject) => {
      let parents = receivers?.parents;
      let persons = receivers?.persons;
      if (data.type.length == 1) {
        let params = {
          schoolType: 2,
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getGradeList(params, header).then((res) => {
          console.log("API-获取年级列表（中小学）:", res, "\nparams:", params);
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = parents?.findIndex(
                (pItem) => pItem.gradeId == item.gradeId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                //有选中,去掉接口
                parents.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.gradeName,
                subData: undefined,
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      } else if (data.type.length == 2) {
        let params = {
          schoolType: 2,
          sysId: getInfo.getSysId(),
          gradeId: data.Info.gradeId,
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getClassList(params, header).then((res) => {
          console.log("API-获取班级列表（中小学）:", res, "\nparams:", params);
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = parents?.findIndex(
                (pItem) => pItem.classId == item.classId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                //有选中,去掉接口
                parents.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.className,
                subData: undefined,
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      } else if (data.type.length == 3) {
        let params = {
          schoolType: 1,
          classId: data.Info.classId,
          schoolId: getInfo.getSchoolId(),
          // classType:data.Info.classType
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getUserParents(params, header).then((res) => {
          console.log("API-获取家长用户（中小学）:", res, "\nparams:", params);
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = persons?.findIndex(
                (pItem) => pItem.userId == item.userId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                persons.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.userName,
                subData: [],
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      }
    });
  },

  getDepartment(data, receivers, fnSet) {
    return new Promise<any>((resolve, reject) => {
      let departments = receivers?.departments;
      let persons = receivers?.persons;
      if (data.type.length == 1) {
        let params1 = {
          sysId: getInfo.getSysId(),
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getDepartmentList(params1, header).then((res1) => {
          console.log(
            "API-获取学校组织结构（中小学）:",
            res1,
            "\nparams:",
            params1
          );
          if (res1.code == 200) {
            let fnArr: any = [];
            const result = res1.data;
            formatData(result);
            result.map((item, index) => {
              let hasCho = 0;
              let idIndex = departments?.findIndex(
                (pItem) => pItem.departmentId == item.departmentId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                departments.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.name,
                subData: item.subData,
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item.Info,
                nodeType: item.nodeType || "",
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      } else {
        let params1 = {
          sysId: getInfo.getSysId(),
          schoolId: getInfo.getSchoolId(),
          departmentId: data.Info.departmentId,
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getDepartmentList(params1, header).then((res1) => {
          console.log(
            "API-获取部门列表（中小学）:",
            res1,
            "\nparams:",
            params1
          );
          if (res1.code == 200) {
            let fnArr: any = [];
            res1.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = departments?.findIndex(
                (pItem) => pItem.departmentId == item.departmentId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                departments.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.departmentName,
                subData: undefined,
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            let header = {
              Authorization: getInfo.getToken(),
            };
            API_getDepartmentStaffs(params1, header).then((res2) => {
              console.log(
                "API-获取部门员工（中小学）:",
                res2,
                "\nparams:",
                params1
              );
              if (res2.code == 200) {
                res2.data.map((item, index) => {
                  let hasCho = 0;
                  let idIndex = persons?.findIndex(
                    (pItem) => pItem.userId == item.userId
                  );
                  if (idIndex == undefined || idIndex == -1) {
                    if (data.hasCho == 1) {
                      //父项，是否选中
                      hasCho = 1;
                    } else {
                      hasCho = 0;
                    }
                  } else {
                    persons.splice(idIndex, 1);
                    hasCho = 1;
                  }
                  let typeArr = JSON.parse(JSON.stringify(data.type));
                  typeArr.push(index);
                  fnArr.push({
                    name: item.userName,
                    subData: [],
                    hasCho: 0,
                    index,
                    type: typeArr,
                    Info: item,
                  });
                });
                fnSet(receivers);
                resolve(fnArr);
              } else {
                fnSet(receivers);
                resolve(fnArr);
              }
            });
          } else {
            resolve([]);
          }
        });
      }
    });
  },

  getMyClass(data, receivers, fnSet) {
    return new Promise<any>((resolve, reject) => {
      let students = receivers?.students;
      let persons = receivers?.persons;
      if (data.type.length == 1) {
        let params = {
          schoolType: getInfo.getSchoolType(),
          userId: getInfo.getUserId(),
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getClassMine(params, header).then((res) => {
          console.log(
            "API-获取我的班级列表（中小学）:",
            res,
            "\nparams:",
            params
          );
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = students?.findIndex(
                (pItem) => pItem.classId == item.classId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                students.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.className,
                subData: undefined,
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      } else if (data.type.length == 2) {
        let params = {
          schoolType: getInfo.getSchoolType(),
          classId: data.Info.classId,
          classType: data.Info.classType,
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getUserStudents(params, header).then((res) => {
          console.log("API-获取学生用户（中小学）:", res, "\nparams:", params);
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = persons?.findIndex(
                (pItem) => pItem.userId == item.userId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                persons.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.userName,
                subData: [],
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      }
    });
  },

  getMyClassParents(data, receivers, fnSet) {
    return new Promise<any>((resolve, reject) => {
      let parents = receivers?.parents;
      let persons = receivers?.persons;
      if (data.type.length == 1) {
        let params = {
          schoolType: getInfo.getSchoolType(),
          userId: getInfo.getUserId(),
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getClassMine(params, header).then((res) => {
          console.log(
            "API-获取我的班级列表-学生家长（中小学）:",
            res,
            "\nparams:",
            params
          );
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = parents?.findIndex(
                (pItem) => pItem.classId == item.classId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                parents.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.className,
                subData: undefined,
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      } else if (data.type.length == 2) {
        let params = {
          schoolType: 1,
          classId: data.Info.classId,
          classType: data.Info.classType,
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getUserParents(params, header).then((res) => {
          console.log("API-获取家长用户（中小学）:", res, "\nparams:", params);
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = persons?.findIndex(
                (pItem) => pItem.userId == item.userId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                persons.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.userName,
                subData: [],
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      }
    });
  },

  getTeacherGroup(data, receivers, fnSet) {
    return new Promise<any>((resolve, reject) => {
      let teachers = receivers?.teachers;
      let persons = receivers?.persons;
      if (data.type.length == 1) {
        let params = {
          schoolType: getInfo.getSchoolType(),
          userId: getInfo.getUserId(),
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getGroupMine(params, header).then((res) => {
          console.log(
            "API-获取我的教师组（中小学）:",
            res,
            "\nparams:",
            params
          );
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = teachers?.findIndex(
                (pItem) => pItem.subjectId == item.subjectId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                teachers.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.subjectName,
                subData: undefined,
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      } else if (data.type.length == 2) {
        let params = {
          schoolType: getInfo.getSchoolType(),
          groupId: data.Info.subjectId,
          schoolId: getInfo.getSchoolId(),
        };
        let header = {
          Authorization: getInfo.getToken(),
        };
        API_getUserTeachers(params, header).then((res) => {
          console.log("API-获取教师用户（中小学）:", res, "\nparams:", params);
          if (res.code == 200 && res.data.length != 0) {
            let fnArr: any = [];
            res.data.map((item, index) => {
              let hasCho = 0;
              let idIndex = persons?.findIndex(
                (pItem) => pItem.userId == item.userId
              );
              if (idIndex == undefined || idIndex == -1) {
                if (data.hasCho == 1) {
                  //父项，是否选中
                  hasCho = 1;
                } else {
                  hasCho = 0;
                }
              } else {
                persons.splice(idIndex, 1);
                hasCho = 1;
              }
              let typeArr = JSON.parse(JSON.stringify(data.type));
              typeArr.push(index);
              fnArr.push({
                name: item.userName,
                subData: [],
                hasCho: hasCho,
                index,
                type: typeArr,
                Info: item,
              });
            });
            fnSet(receivers);
            resolve(fnArr);
          } else {
            resolve([]);
          }
        });
      }
    });
  },

  getGrade(data, receivers, fnSet) {
    //获取年级班级(*特殊处理)
    return new Promise<any>((resolve, reject) => {
      let grades = receivers?.grades;
      let params = {
        schoolType: getInfo.getSchoolType(),
        schoolId: getInfo.getSchoolId(),
      };
      let header = {
        Authorization: getInfo.getToken(),
      };
      API_getGradeList(params, header).then((res) => {
        console.log("API-获取年级列表:", res, "\nparams:", params);
        if (res.code == 200) {
          let handleRes = res.data.map((item, index) => {
            let hasCho = 0;
            let idIndex = grades?.findIndex(
              (pItem) => pItem.gradeId == item.gradeId
            );
            if (idIndex == undefined || idIndex == -1) {
              if (data.hasCho == 1) {
                //父项，是否选中
                hasCho = 1;
              } else {
                hasCho = 0;
              }
            } else {
              grades.splice(idIndex, 1);
              hasCho = 1;
            }
            return {
              name: item.gradeName,
              subData: undefined,
              hasCho: hasCho,
              index,
              type: [index],
              Info: item,
            };
          });

          fnSet(receivers);
          resolve(handleRes);
        } else {
          resolve([]);
        }
      });
    });
  },

  getClass(data, receivers, fnSet) {
    //获取班级(*特殊处理)
    return new Promise<any>((resolve, reject) => {
      let classes = receivers?.classes;
      let params: any = {
        schoolType: getInfo.getSchoolType(),
        sysId: getInfo.getSysId(),
        schoolId: getInfo.getSchoolId(),
        // gradeId:data.Info.gradeId,
      };
      if (getInfo.getSchoolType() == 1) {
        //大学(传专业ID)
        params.majorId = data.Info.majorId;
      } else {
        //中小学（传年级ID）
        params.gradeId = data.Info.gradeId;
      }
      let header = {
        Authorization: getInfo.getToken(),
      };
      API_getClassList(params, header).then((res) => {
        console.log("API-获取班级列表:", res, "\nparams:", params);
        if (res.code == 200) {
          let handleRes = res.data.map((item, index) => {
            let hasCho = 0;
            let idIndex = classes?.findIndex(
              (pItem) => pItem.classId == item.classId
            );
            if (idIndex == undefined || idIndex == -1) {
              if (data.hasCho == 1) {
                //父项，是否选中
                hasCho = 1;
              } else {
                hasCho = 0;
              }
            } else {
              classes.splice(idIndex, 1);
              hasCho = 1;
            }
            return {
              name: item.className,
              subData: undefined,
              hasCho: hasCho,
              index,
              type: [...(data.type || []), index],
              Info: item,
            };
          });
          fnSet(receivers);
          resolve(handleRes);
        } else {
          resolve([]);
        }
      });
    });
  },

};

//建立索引
function addIndexToTree(tree, indexArray = []) {
  tree.type = indexArray;
  if (tree.subData) {
      tree.subData.forEach((child, index) => {
          addIndexToTree(child, [...indexArray, index]);
      });
  }
}

export function formatData(data,departments=[],persons=[],indexArray :number[]=[]) {
  if (!Array.isArray(data)) {
    return;
  }
  data.forEach((item,index) => {
    item.name = item.departmentName || item.userName;
    item.Info = JSON.parse(JSON.stringify(item));

    if(item.userId){
      const id=item.userId
      const fIndex=persons.findIndex(p=>p.userId==id)
  
      //如果当前有命中
      if(fIndex>-1){
        item.hasCho=1
      }else{
        item.hasCho=0
      }
    }

    if (item.structureVoList || item.selectObjectUserVoList) {
      const id=item.departmentId
      const fIndex=departments.findIndex(d=>d.departmentId==id)
  
      //如果当前有命中
      if(fIndex>-1){
        departments.splice(fIndex, 1);
        item.hasCho=1
      }else{
        item.hasCho=0
      }

      const child = item.structureVoList || item.selectObjectUserVoList;
      item.subData = child;
      addIndexToTree(item,[...indexArray,index])

      formatData(child,departments,persons,item.type);
    }else{
      item.subData = [];
    }
  });
}
